const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs');

const unirest = require('unirest');

module.exports = (shops) => {
    puppeteer.launch({executablePath: '/Users/hui/Downloads/chrome-mac/Chromium.app/Contents/MacOS/Chromium',headless: false}).then(async browser => {
        // for (let index = 0; index < shops.length; index++) {
        //     var shop = shops[index];
        //     var p = path.join(process.cwd(), 'shops', `shop_${shop.shopId}_comment.json`);    
        //     await sleep(5000);
        //     if (!fs.existsSync(p)) {
        //         const writeStream = fs.createWriteStream(p);
        //         console.log('not exists!');
        //         var callback = function (res) {
        //             if (res.ok) {
        //                 var req = res.request();
        //                 if (req.url.startsWith('http://www.dianping.com/ajax/json/shopDynamic/allReview')) {
        //                     res.json().then((json) => {
        //                         console.log(json);
        //                         writeStream.write(JSON.stringify(json));
        //                         writeStream.end();
        //                     });
        //                 }
        //             }
        //         };                
        //         await excute(shop.shopUrl, browser,callback);
        //     } 
        // }
        var users = [];
        // const writeStream2 = fs.createWriteStream(path.join(process.cwd(), 'members', `members.json`));
        for (let index = 0; index < shops.length; index++) {
            var shop = shops[index];
            var p = path.join(process.cwd(), 'shops', `shop_${shop.shopId}_comment.json`);
            var shopInfoString = fs.readFileSync(p).toString();
            if (shopInfoString) {
                var shopInfo = JSON.parse(shopInfoString);
                if (shopInfo && shopInfo.reviewAllDOList && shopInfo.reviewAllDOList.length > 0) {
                    console.log(`index: $ {index}   shopId:${shop.shopId} :   ${shopInfo.reviewAllDOList.length}`);
                    for (let index = 0; index < shopInfo.reviewAllDOList.length; index++) {
                        var review = shopInfo.reviewAllDOList[index];
                        //let memberUrl = `http://www.dianping.com/member/${review.user.userId}`;
                        //console.log(review.user.userId);
                        // const page = await browser.newPage();
                        // //page.on('response', callback);
                        // await page.goto(memberUrl);
                        // const datas = await page.evaluate(() => {
                        //     const anchors = Array.from(document.querySelectorAll(".col-link"));
                        //     return anchors.map(anchor => anchor.textContent);
                        // });
                        // users.push({
                        //     userId: review.user.userId,
                        //     userNickName: review.user.userNickName,
                        //     userFace: review.user.userFace,
                        //     userPower: review.user.userPower,
                        //     datas: datas
                        // });
                        
                        let member = {
                            userId: review.user.userId.toString(),
                            userNickName: review.user.userNickName,
                            userFace: review.user.userFace,
                            userPower: review.user.userPower,
                            vipLevel: review.vipLevel
                            // datas: datas
                        };

                        // unirest.put(`http://127.0.0.1:7001/shop/${shop.shopId}`)
                        // .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                        // .send({
                        //     dishTagStrList: shopInfo.dishTagStrList,
                        //     reviewCountPic: shopInfo.reviewCountPic,
                        //     reviewCountAll: shopInfo.reviewCountAll,
                        // })
                        // .end(function (response) {
                        //     console.log(response.body);
                        // });  

                        unirest.post('http://127.0.0.1:7001/member')
                        .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                        .send(member)
                        .end(function (response) {
                            console.log(response.body);
                        });  
                        // writeStream2.write(JSON.stringify(member));
                        
                        // console.log('datas:', datas);
                        // await sleep(1000);
                        // await page.close();
                    }
                }
            }
        }
        writeStream2.end();
        await browser.close();
    });
}


async function excute(shopUrl, browser, callback) {
    const page = await browser.newPage();
    page.on('response', callback);
    await page.goto(shopUrl);
    await sleep(5000);
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}
